package com.platform.modules.chat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.platform.common.constant.AppConstants;
import com.platform.common.exception.BaseException;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.utils.redis.RedisUtils;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.chat.dao.ChatApplyDao;
import com.platform.modules.chat.dao.ChatGroupMonitorDao;
import com.platform.modules.chat.domain.ChatApply;
import com.platform.modules.chat.domain.ChatGroup;
import com.platform.modules.chat.domain.ChatGroupMonitor;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.enums.ApplySourceEnum;
import com.platform.modules.chat.enums.ApplyStatusEnum;
import com.platform.modules.chat.enums.ApplyTypeEnum;
import com.platform.modules.chat.enums.RiskTypeEnum;
import com.platform.modules.chat.service.ChatGroupInfoService;
import com.platform.modules.chat.service.ChatGroupMonitorService;
import com.platform.modules.chat.service.ChatGroupService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.chat.vo.ApplyVo02;
import com.platform.modules.chat.vo.ApplyVo03;
import com.platform.modules.push.enums.PushNoticeTypeEnum;
import com.platform.modules.push.service.ChatPushService;
import com.platform.modules.push.vo.PushParamVo;
import com.platform.modules.wallet.domain.WalletRedPacket;
import com.platform.modules.wallet.domain.WalletRedPacketSub;
import com.platform.modules.wallet.service.WalletRedPacketService;
import com.platform.modules.wallet.service.WalletRedPacketSubService;
import com.platform.modules.wallet.service.WalletTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 好友申请表 服务层实现
 * q3z3
 * </p>
 */
@Service("chatGroupMonitorService")
public class ChatGroupMonitorServiceImpl extends BaseServiceImpl<ChatGroupMonitor> implements ChatGroupMonitorService {

    @Resource
    private ChatGroupMonitorDao chatGroupMonitorDao;

    @Resource
    private ChatGroupService groupService;

    @Resource
    private WalletRedPacketService redPacketService;
    @Resource
    private WalletTransferService transferService;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(chatGroupMonitorDao);
    }

    private ChatGroupMonitor create(Long groupId) {
        ChatGroupMonitor groupMonitor = new ChatGroupMonitor();
        groupMonitor.setGroupId(groupId);
        groupMonitor.setProfit(0.00f);
        groupMonitor.setUp(0.00f);
        groupMonitor.setDown(0.00f);
        groupMonitor.setInTotal(0);
        groupMonitor.setInBalance(0);
        groupMonitor.setOutTotal(0);
        groupMonitor.setOutBalance(0);
        groupMonitor.setUpdateTime(DateUtil.date());
        groupMonitor.setRisk(RiskTypeEnum.NORMAL);
        add(groupMonitor);
        return groupMonitor;
    }

    public ChatGroupMonitor getByGroupId(Long groupId) {

        ChatGroupMonitor groupMonitor = queryOne(new ChatGroupMonitor().setGroupId(groupId));

        if (ObjectUtil.isEmpty(groupMonitor)) {
            groupMonitor = create(groupId);
        }
        return groupMonitor;
    }

    @Override
    public void updateTotal(Long groupId) {

        ChatGroupMonitor groupMonitor = getByGroupId(groupId);

        List<Long> userIds = new ArrayList<>();

        if (StrUtil.isNotBlank(groupMonitor.getNotUserId())) {

            String[] split = groupMonitor.getNotUserId().split(",");

            for (String s : split) {
                userIds.add(Long.parseLong(s));
            }
        }

        int intotal = redPacketService.queryTodayNotUserId(groupId, userIds);

        groupMonitor.setInTotal(intotal);
        if (ObjectUtil.isNotEmpty(groupMonitor.getFinance())) {
            groupMonitor.setOutTotal(transferService.queryGroupTodayNotUserId(groupMonitor.getFinance(), userIds));
        } else {
            groupMonitor.setOutTotal(0);
        }

        if (null == groupMonitor.getInBalance()) groupMonitor.setInBalance(0);
        if (null == groupMonitor.getOutBalance()) groupMonitor.setOutBalance(0);
        int in = groupMonitor.getInTotal() - groupMonitor.getInBalance();
        int out = groupMonitor.getOutTotal() - groupMonitor.getOutBalance();

        if (in > 0 && out > 0) {
            BigDecimal profit = new BigDecimal(in - out).divide(new BigDecimal(out), 3, BigDecimal.ROUND_DOWN);
            groupMonitor.setProfit(profit.floatValue());
        } else {
            groupMonitor.setProfit(1.000f);
        }

        if (groupMonitor.getProfit().floatValue() == 1.000f ||
                groupMonitor.getUp().floatValue() == 0.000f ||
                groupMonitor.getDown().floatValue() == 0.000f) {
            groupMonitor.setRisk(RiskTypeEnum.NORMAL);
        } else {

            if (groupMonitor.getProfit() < groupMonitor.getDown()) {
                groupMonitor.setRisk(RiskTypeEnum.NO);
            } else if (groupMonitor.getProfit() > groupMonitor.getUp()) {
                groupMonitor.setRisk(RiskTypeEnum.YES);
            } else {
                groupMonitor.setRisk(RiskTypeEnum.NORMAL);
            }
        }

        groupMonitor.setUpdateTime(DateUtil.date());
        updateById(groupMonitor);
    }
}
